package com.mhkj.base.rbac.role.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.mhkj.base.rbac.role.dao.MyRoleDao;
import com.mhkj.base.rbac.role.entity.SystemRole;
import com.mhkj.base.rbac.role.service.MyRoleService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.rbac.service.DictionaryService;

@Service
public class MyRoleServiceImpl extends BaseServiceImpl<SystemRole> implements MyRoleService {
	@Resource
	private MyRoleDao roleDao;
	@Resource
	private DictionaryService dictionaryService;
	@Resource
	public void setRoleDao(MyRoleDao RoleDao) {
		super.setBaseRepository(RoleDao);
	}

	
	
	
	@Override
	public Pagination<SystemRole> listByPage(SystemRole role, int pageIndex, int size ) {
		Specification<SystemRole> spec = new Specification<SystemRole>() {
			/**
			 * 序列号	
			 */
			private static final long serialVersionUID = -786003485941679959L;

			@Override
			public Predicate toPredicate(Root<SystemRole> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				if(StringUtils.hasText(role.getRoleName())){
	                   Predicate p1=criteriaBuilder.like(root.get("roleName").as(String.class), "%"+role.getRoleName()+"%");
	                   lstPredicates.add(p1);
				}
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Page<SystemRole> pageT = roleDao.findAll(spec, PageRequest.of(pageIndex, size, Sort.Direction.ASC, "id"));
		Pagination<SystemRole> pagination = new Pagination<SystemRole>();
		pagination.setTotal(roleDao.count(spec));
		pagination.setData(pageT.getContent());
		return pagination;
	}




	@Override
	public List<Object[]> listItemByRole(Long id) {
		return roleDao.listItemByRole(id);
	}
 
}


